package cn.hahah.module.blog.service.userdialogue;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userdialogue.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialogue.UserDialogueDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userdialogue.UserDialogueMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户对话 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserDialogueServiceImpl implements UserDialogueService {

    @Resource
    private UserDialogueMapper userDialogueMapper;

    @Override
    public Long createUserDialogue(UserDialogueSaveReqVO createReqVO) {
        // 插入
        UserDialogueDO userDialogue = BeanUtils.toBean(createReqVO, UserDialogueDO.class);
        userDialogueMapper.insert(userDialogue);
        // 返回
        return userDialogue.getId();
    }

    @Override
    public void updateUserDialogue(UserDialogueSaveReqVO updateReqVO) {
        // 校验存在
        validateUserDialogueExists(updateReqVO.getId());
        // 更新
        UserDialogueDO updateObj = BeanUtils.toBean(updateReqVO, UserDialogueDO.class);
        userDialogueMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserDialogue(Long id) {
        // 校验存在
        validateUserDialogueExists(id);
        // 删除
        userDialogueMapper.deleteById(id);
    }

    private void validateUserDialogueExists(Long id) {
        if (userDialogueMapper.selectById(id) == null) {
            throw exception(USER_DIALOGUE_NOT_EXISTS);
        }
    }

    @Override
    public UserDialogueDO getUserDialogue(Long id) {
        return userDialogueMapper.selectById(id);
    }

    @Override
    public PageResult<UserDialogueDO> getUserDialoguePage(UserDialoguePageReqVO pageReqVO) {
        return userDialogueMapper.selectPage(pageReqVO);
    }

}